﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DBSourceToolsLib.DatabaseProjectUI
{
    public partial class DatabaseListForm : Form
    {
        private DBConnectionLib.Database m_Database;
        public string DatabaseName;

        public DatabaseListForm(DBConnectionLib.Database oDatabase)
        {
            InitializeComponent();
            m_Database = oDatabase;
            this.Text = "Databases on Server " + oDatabase.DataSourceName;
        }

        private void m_buttonOk_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void m_buttonCancel_Click(object sender, EventArgs e)
        {

        }

        private void DatabaseListForm_Load(object sender, EventArgs e)
        {
            try
            {
                Microsoft.SqlServer.Management.Smo.Server oServer = m_Database.GetSmoServer();
                foreach (Microsoft.SqlServer.Management.Smo.Database oDb in oServer.Databases)
                {
                    this.m_listBoxDatabases.Items.Add(oDb.Name);
                }
            }
            catch (Exception ex)
            {
                DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm oErr = new DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm(ex.ToString());
                oErr.ShowDialog(this);
            }

        }


        private void m_listBoxDatabases_SelectedIndexChanged(object sender, EventArgs e)
        {
            DatabaseName = m_listBoxDatabases.SelectedItem.ToString();
        }

        private void m_btnCancel_Click(object sender, EventArgs e)
        {
            this.DatabaseName = "";
            this.Close();
        }
    }
}
